package be.atc.phh.servlets;

import java.io.IOException;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;


import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.*;

import org.joda.time.DateTime;
import org.joda.time.Days;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.joda.time.Days;
import be.atc.antoine.beans.ListePlanUF;
import be.atc.antoine.beans.Vacance;
import be.atc.phh.beans.ListeUfOrgaDateEtNom;
import be.atc.phh.models.ModelCalendrier;
import be.atc.phh.models.ModelListeUfOrgaDateEtNom;
import be.atc.antoine.beans.Calendrier;
import be.atc.ferrari.beans.Professeur;
import be.atc.ferrari.modelProfesseur.ModelProfesseur;
import be.atc.antoine.beans.Local;
import be.atc.antoine.modelListePlanUF.ModelListePlanUF;
import be.atc.antoine.modelLocal.ModelLocal;
/**
 * Servlet implementation class Calendrier
 */
@WebServlet("/Calendrier")
public class ServletCalendrier extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private int k;
	private int i;
	//private DateTime dates;
	private DateTime dt2;
	private DateTime dt1;
	List<DateTime> dates = new ArrayList<DateTime>();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletCalendrier() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public void init (){
    	             
    	ServletContext servletContext=getServletContext();

		List<Calendrier> dateInOut = (List<Calendrier>) servletContext.getAttribute("dateInOut");
		
		Calendar debutAnneScolaire =Calendar.getInstance();
		Calendar finAnneScolaire = Calendar.getInstance();
		
		for (Calendrier datefor : dateInOut){
		 debutAnneScolaire.setTime(datefor.getDateDebutCal());
		 finAnneScolaire.setTime(datefor.getDateFinCal());
		}

		//Integer yearDebut = debutAnneScolaire.get(Calendar.YEAR);
		//Integer yearFin = finAnneScolaire.get(Calendar.YEAR);
		DateTime dt1 = new DateTime(debutAnneScolaire);
        DateTime dt2 = new DateTime(finAnneScolaire);
       
            
        
       int i=1;

        switch (dt1.dayOfWeek().getAsText(Locale.FRENCH))
        {
          case "lundi":
        	  i=1;
            break;
          case "mardi":
        	  i=2;
        	break;
          case "mercredi":
        	  i=3;
          	break;
          case "jeudi":
        	  i=4;
          	break;
          case "vendredi":
        	  i=5;
          	break;
          case "samedi":
        	  i=6;
          	break;
          case "dimanche":
        	  i=7;
          	break;
         
          default:
            
        }
        int k=2;
        switch (dt2.dayOfWeek().getAsText(Locale.FRENCH))
        {
          case "lundi":
        	  k=8;
            break;
          case "mardi":
        	  k=7;
        	break;
          case "mercredi":
        	  k=6;
          	break;
          case "jeudi":
        	  k=5;
          	break;
          case "vendredi":
        	  k=4;
          	break;
          case "samedi":
        	  k=3;
          	break;
          case "dimanche":
        	  k=2;
          	break;
         
          default:
            
        }
        
        int days = Days.daysBetween(dt1, dt2).getDays();
        
        
        
		 for(int j = 0; j <= days; j++) {
			 if((dt2.dayOfWeek().getAsText(Locale.FRENCH)) != "dimanche")
			dates.add(dt1.plusDays(j));
		 }   	 
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		HttpSession session=request.getSession();
		//HttpSession session=request.getSession();
		
		//request.setAttribute("calendrierdate", cal);
		//request.setAttribute("calendrierdateavecconge", dateAnneScolaire);

		//List<ListePlanUF> listPlanUF = ModelListePlanUF.trouverByIdListePlanUF(1);
		ServletContext servletContext=getServletContext();
		List<Vacance> conge = (List<Vacance>) servletContext.getAttribute("listcongescolaire");

		//List<ListePlanUF> listPlanUF = ModelListePlanUF.trouverByIdListePlanUF(1);

		request.setAttribute("kstart", k);
		request.setAttribute("istart", i);
		session.setAttribute("dates", dates);
		request.setAttribute("dates", dates);
		servletContext.setAttribute("anneedebut", dt2);
		servletContext.setAttribute("anneefin", dt1);

		request.setAttribute("conge", conge);
		//request.setAttribute("listPlanUF", listPlanUF);

		//request.setAttribute("listPlanUF", listPlanUF);
		

		
		this.getServletContext().getRequestDispatcher( "/JSP/calendrier.jsp" ).forward( request, response );
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String varIn = request.getParameter("choixSectionAnnee");
		HttpSession session=request.getSession();
		int id = Integer.parseInt(varIn);
		session.setAttribute("sectionID", id);
		System.out.println(" request "+request.getParameter("choixSectionAnnee"));
		List<ListePlanUF> listPlanUF = ModelListePlanUF.trouverByIdListePlanUF(id);
		List<ListePlanUF> listPlanUFSeul = ModelListePlanUF.trouverAllListePlanUFBySectionAnnee(id);
		String title = null;
		for (ListePlanUF i : listPlanUF){
			title ="SECTION "+i.getPlanningUF().getSectionAnnee().getAnneeSection()+" - "+
			i.getPlanningUF().getUFOrga().getSectionSecteur().getLibelleSectionSecteur();			
		}

		request.setAttribute("kstart", k);
		request.setAttribute("title", title);
		request.setAttribute("istart", i);
		request.setAttribute("dates", dates);
		request.setAttribute("dt2", dt2);
		request.setAttribute("idsection", id);
		request.setAttribute("listPlanUF", listPlanUF);
		request.setAttribute("listPlanUFSeul", listPlanUFSeul);
		
		this.getServletContext().getRequestDispatcher( "/JSP/calendrier.jsp" ).forward( request, response );
	}

}
